package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.UserinfoEntity;
import com.jifeng.br.pjj.bookreader.entity.VipEntity;
import com.jifeng.br.pjj.bookreader.mapper.VipMapper;
import com.jifeng.br.pjj.bookreader.service.CodeService;
import com.jifeng.br.pjj.bookreader.service.UserinfoService;
import com.jifeng.br.pjj.bookreader.service.VipService;
import com.jifeng.br.pjj.bookreader.util.R;
import com.jifeng.br.pjj.bookreader.util.exception.RTException;
import com.jifeng.br.pjj.bookreader.util.form.LoginForm;
import com.jifeng.br.pjj.bookreader.util.form.RegisterForm;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 会员信息表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@Service
@Transactional
public class VipServiceImpl extends ServiceImpl<VipMapper, VipEntity> implements VipService {
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private CodeService codeService;

    @Override
    public List<VipEntity> getAuthorSelect() {
        return vipMapper.getVipByRole(2);
    }

    @Override
    public R login(LoginForm loginForm) {
        UserinfoEntity loginUser = userinfoService.getByUsername(loginForm.getUsername());
        String md5Pwd = DigestUtils.md5Hex(loginForm.getPassword());
        if (loginUser == null || !loginUser.getPassword().equals(md5Pwd)) {
            return R.error("用户名或密码不正确!");
        }
        if (loginUser.getRole() == 1) {
            return R.error("管理员禁止登录前台！");
        }
        if (loginUser.getStatus() == 2) {
            return R.error("该用户已被封号，请联系管理员！");
        }
        VipEntity vip = this.getOne(new QueryWrapper<VipEntity>().eq("user_id", loginUser.getUserId()));
        return R.data(vip);
    }

    @Override
    public void register(RegisterForm registerForm) {
        if (!codeService.checkKaptcha(registerForm.getUuid(), registerForm.getCode())) {
            throw new RTException("验证码不正确");
        }
        if (userinfoService.getByUsername(registerForm.getPhone()) != null) {
            throw new RTException("该手机号已注册，请直接登录！");
        }
        UserinfoEntity userinfo = new UserinfoEntity();
        userinfo.setUsername(registerForm.getPhone());
        userinfo.setPassword(DigestUtils.md5Hex(registerForm.getPassword()));
        userinfo.setPhone(registerForm.getPhone());
        userinfo.setRole(3);
        userinfo.setNickname(registerForm.getNickname());
        userinfoService.save(userinfo);

        VipEntity vip = new VipEntity();
        vip.setUserId(userinfo.getUserId());
        vip.setName(registerForm.getNickname());
        vip.setSex(registerForm.getSex());
        vip.setAvatar(registerForm.getAvatar());
        this.save(vip);

    }

    @Override
    public void updateVip(VipEntity vip) {
        this.updateById(vip);
        UserinfoEntity userinfo = new UserinfoEntity();
        userinfo.setUserId(vip.getUserId());
        userinfo.setNickname(vip.getName());
        userinfoService.updateById(userinfo);
    }

    @Override
    public void updateAuthor(VipEntity vipEntity) {
        VipEntity vip = new VipEntity();
        vip.setVipId(vipEntity.getVipId());
        vip.setAuthorStatus(1);
        this.updateById(vip);
        UserinfoEntity userinfo = new UserinfoEntity();
        userinfo.setUserId(vipEntity.getUserId());
        userinfo.setRole(2);
        userinfoService.updateById(userinfo);
    }
}
